package com.permissions.dispatcher.processor;

import com.google.auto.service.AutoService;
import com.permissions.dispatcher.annotation.RuntimePermissions;
import com.permissions.dispatcher.processor.impl.AbilityProcessorUnit;
import com.permissions.dispatcher.processor.impl.AbilitySliceProcessorUnit;
import com.permissions.dispatcher.processor.util.Extensions;
import com.squareup.javapoet.JavaFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import javax.annotation.processing.AbstractProcessor;
import javax.annotation.processing.Filer;
import javax.annotation.processing.ProcessingEnvironment;
import javax.annotation.processing.Processor;
import javax.annotation.processing.RoundEnvironment;
import javax.lang.model.SourceVersion;
import javax.lang.model.element.Element;
import javax.lang.model.element.TypeElement;
import javax.lang.model.util.Elements;
import javax.lang.model.util.Types;

/**
 * 注解处理器核心类，生成java代码和文件
 */
@AutoService(Processor.class)
public class PermissionsProcessor extends AbstractProcessor {
    private Filer filer;

    /**
     * 方法块
     */
    public static Elements ELEMENT_UTILS;
    /**
     * 类型
     */
    public static Types TYPE_UTILS;

    private List<ProcessorUnit<JavaFile>> javaProcessorUnits;

    @Override
    public synchronized void init(ProcessingEnvironment processingEnv) {
        super.init(processingEnv);
        filer = processingEnv.getFiler();
        ELEMENT_UTILS = processingEnv.getElementUtils();
        TYPE_UTILS = processingEnv.getTypeUtils();
        javaProcessorUnits = new ArrayList<>();
        javaProcessorUnits.add(new AbilityProcessorUnit());
        javaProcessorUnits.add(new AbilitySliceProcessorUnit());
    }

    @Override
    public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnvironment) {
        RequestCodeProvider requestCodeProvider = new RequestCodeProvider();
        Set<? extends Element> set = roundEnvironment.getElementsAnnotatedWith(RuntimePermissions.class);
        for (Element element : set) {
            // 下面这句太重要了
            RuntimePermissionsElement permissionsElement = new RuntimePermissionsElement((TypeElement) element);
            processJava(element, permissionsElement, requestCodeProvider);
        }

        return true;
    }

    private void processJava(
            Element element, RuntimePermissionsElement permissionsElement, RequestCodeProvider requestCodeProvider) {
        ProcessorUnit unit = Extensions.findAndValidateProcessorUnit(javaProcessorUnits, element);
        JavaFile javaFile = (JavaFile) unit.createFile(permissionsElement, requestCodeProvider);


        try {
            javaFile.writeTo(filer);
        } catch (IOException e) {
            String error = e.getMessage();
        }
    }

    // 重写方法方式
    @Override
    public SourceVersion getSupportedSourceVersion() {
        return SourceVersion.latestSupported();
    }

    @Override
    public Set<String> getSupportedAnnotationTypes() {
        return Collections.singleton(RuntimePermissions.class.getCanonicalName());
    }
}
